package com.hilin.common.util;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @author qhl
 */
public class ThreadPoolUtils {

    /**
     * 存放线程池，key：名称， value：线程池
     */
    private static final Map<String, ThreadPoolExecutor> POOL_EXECUTORS = new HashMap<>();

    /**
     * 创建线程池
     *
     * @param name            线程池名称
     * @param corePoolSize    核心线程数
     * @param maximumPoolSize 最大线程数
     * @param keepAliveTime   线程空闲时间
     * @param workQueue       等待队列
     */
    public static ThreadPoolExecutor createThreadPool(String name, int corePoolSize, int maximumPoolSize, int keepAliveTime, int workQueue) {
        if (!POOL_EXECUTORS.containsKey(name)) {
            POOL_EXECUTORS.put(name, new ThreadPoolExecutor(corePoolSize, maximumPoolSize,
                    keepAliveTime,
                    TimeUnit.SECONDS,
                    new LinkedBlockingQueue<>(workQueue),
                    new ThreadPoolExecutor.DiscardPolicy()));
        }
        return POOL_EXECUTORS.get(name);
    }

}
